import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import { LoginParamsType } from "../../types";
import { login } from "../../api";
import { encryptedData } from "../../utils";
const initialState = {
  userInfo: {
    departmentId: -1,
    departmentName: "",
    doctorId: -1,
    imagePic: "",
    inauguralHospital: "",
    jiGuangPwd: "",
    jobTitle: "",
    name: "",
    reviewStatus: -1,
    sessionId: "",
    userName: "",
    whetherHaveImagePic: -1,
  },
  isLogin: false
};

export const userLogin = createAsyncThunk(
  "user/userLogin",
  async (data: LoginParamsType) => {
    data.pwd = encryptedData(data.pwd);
    const resp = await login(data);
    return resp.data;
  } 
);

const userSlice = createSlice({
  name: "user",
  initialState,
  reducers: {
    quit(state) {
      state.isLogin = false
      state.userInfo = initialState.userInfo
    }
  },
  extraReducers(builder) {
    builder.addCase(userLogin.fulfilled, (state, action) => {
      const { status, message, result } = action.payload;
      if (status === "0000") {
        state.userInfo = result;
        state.isLogin = true
      }
    });
  },
});

export default userSlice;


export const { quit } = userSlice.actions